layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;
    var tableIns = table.render({
        elem: '#list',
        url : '/cooperativeNumber/list/',
        text: { none: "Aucun résultat"},
        cellMinWidth : 95,
        height : "full-125",
        id : "listTable",
        cols : [[
            {field: 'jobNumber', title: "员工编号", align:"center", width: 200},
            {field: 'number', title: "合作商编号", align:"center"},
            {field: 'name', title: "合作商简名", align:"center", width: 300, templet : function( d){
                return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.name + "</div>";
            }},
            {field: 'address', title: "合作商地址", align:"center"},
            {field: 'siren', title: "合作商Siren号码", align:"center"},
            {field: 'nameRule', title: "名称包含", align:"center", width: 300, templet : function( d){
                return "<div style=\"overflow:hidden;white-space:nowrap;text-overflow:ellipsis;\">" + d.nameRule + "</div>";
            }},
            {field: 'codeHt20Rule', title: "HT20", align:"center"},
            {field: 'codeHt10Rule', title: "HT10", align:"center"},
            {field: 'codeHt5Rule', title: "HT5.5", align:"center"},
            {field: 'codeHt2Rule', title: 'HT2.1', align:'center'},
            {field: 'codeHt0Rule', title: 'HT0', align:'center'},
            {title: 'État', templet:'#listBar',fixed: "right", align:"center"}
        ]],
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function( res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.length, //解析数据长度
                "data": res.data //解析数据列表
            };
        }
    });
    var maxHeight = $(window).height();

    //添加/修改/详细查看（readonly=true）
    function addOrUpdateOrDetail( data, readonly){
        var index = layui.layer.open({
            title : "添加",
            type : 2,
            area: [600 + 'px', (maxHeight < 500 ? maxHeight-10 : 500) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id: "cooperativeNumber", //设定一个id，防止重复弹出
            content : "formData.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                if( data){
                    body.find( "input[name=id]").val( data.id).attr( "readonly", readonly);//标识是修改还是新增
                    body.find( "select[name=jobNumber]").val( data.jobNumber).attr( "readonly", readonly);
                    body.find( "input[name=jobNumberId]").val( data.jobNumber).attr( "readonly", readonly);
                    //经销商编号不能修改，否则会引起很多问题
                    body.find( "input[name=number]").val( data.number).attr( "readonly", true);
                    body.find( "input[name=nameRule]").val( data.nameRule).attr( "readonly", readonly);
                    body.find( "input[name=idRule]").val( data.idRule).attr( "readonly", readonly);
                    body.find( "input[name=name]").val( data.name).attr( "readonly", readonly);
                    body.find( "input[name=address]").val( data.address).attr( "readonly", readonly);
                    body.find( "input[name=siren]").val( data.siren).attr( "readonly", readonly);
                    body.find( "input[name=phone]").val( data.phone).attr( "readonly", readonly);
                    body.find( "input[name=taxNumber]").val( data.taxNumber).attr( "readonly", readonly);
                    body.find( "input[name=addr]").val( data.addr).attr( "readonly", readonly);
                    body.find( "input[name=emailCode]").val( data.emailCode).attr( "readonly", readonly);
                    body.find( "input[name=city]").val( data.city).attr( "readonly", readonly);
                    body.find( "input[name=country]").val( data.country).attr( "readonly", readonly);
                    body.find( "input[name=codeHt20Rule]").val( data.codeHt20Rule).attr( "readonly", readonly);
                    body.find( "input[name=codeHt10Rule]").val( data.codeHt10Rule).attr( "readonly", readonly);
                    body.find( "input[name=codeHt5Rule]").val( data.codeHt5Rule).attr( "readonly", readonly);
                    body.find( "input[name=codeHt2Rule]").val( data.codeHt2Rule).attr( "readonly", readonly);
                    body.find( "input[name=codeHt0Rule]").val( data.codeHt0Rule).attr( "readonly", readonly);
                    if( readonly){
                        body.find( "#submitDiv").hide();
                    }
                    form.render();
                }
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //添加
    $(".add_btn").click(function(){
        addOrUpdateOrDetail();
    });

    //搜索
    $("input[name=jobNumber]").on("input propertychange",function(e){
        search();
    });
    $("input[name=number]").on("input propertychange",function(e){
        search();
    });
    $("input[name=name]").on("input propertychange",function(e){
        search();
    });
    $("input[name=nameRule]").on("input propertychange",function(e){
        search();
    });

    function search(){
        var jobNumber = $("input[name=jobNumber]").val();
        var number = $("input[name=number]").val();
        var name = $("input[name=name]").val();
        var nameRule = $("input[name=nameRule]").val();
        table.reload("listTable",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                "jobNumber": jobNumber,
                "number": number,
                "name": name,
                "nameRule": nameRule
            }
        })
    }
    //删除
    function del( id){
        layer.confirm('确定删除选中的数据项？', {icon: 3, title: '提示信息'}, function (index) {
            $.post("/cooperativeNumber/del",{
                "id" : id
            },function( res){
                if( res.code == 200){
                    tableIns.reload();
                    layer.close( index);
                    top.layer.msg( res.msg);
                }else{
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                }
            });
        });
    }

    //列表操作
    table.on('tool(list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addOrUpdateOrDetail( data, false);
        }else if(layEvent === 'look'){ //预览
            addOrUpdateOrDetail( data, true);
        }else if(layEvent === 'del'){
            del( data.id);
        }else if(layEvent === 'show_cooperatice_list'){
            //查看编号对应的复杂名列表
            var index = layui.layer.open({
                title : "复杂名列表",
                area: "600px",
                content : data.cooperativeNames
            });
        }
    });
});
